<?php

/**
 * This is the model class for table "skyn_product".
 *
 * The followings are the available columns in table 'skyn_product':
 * @property integer $prod_id
 * @property string $prod_name
 * @property string $img_name
 * @property string $img_extension
 * @property string $description
 * @property string $short_desc
 * @property string $tag
 */
class Product extends ProductBase
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Product the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'skyn_product';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('prod_name, description, short_desc', 'required'),
			array('prod_name, img_name, img_extension, short_desc', 'length', 'max'=>255),
			array('tag', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('prod_id, prod_name, img_name, img_extension, description, short_desc, tag', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'prod_id' => 'Prod',
			'prod_name' => 'Product Name',
			'img_name' => 'Product Image',
			'img_extension' => 'Prod Img',
			'description' => 'Description',
			'short_desc' => 'Short Description',
			'tag' => 'Tag',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('prod_id',$this->prod_id);
		$criteria->compare('prod_name',$this->prod_name,true);
		$criteria->compare('img_name',$this->img_name,true);
		$criteria->compare('img_extension',$this->img_extension,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('short_desc',$this->short_desc,true);
		$criteria->compare('tag',$this->tag,true);
                $criteria->limit = LIMIT_ADMIN_RECORDS;

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
                        'pagination'=>array(
                            'pageSize'=>LIMIT_ADMIN_RECORDS,
                        ),
		));
	}

        /**
         * Validate thumbnail must be attached in create product
         */
        public function beforeSave() {
            parent::beforeSave();

            //validate thumb when insert new product
            if ($this->isNewRecord && ($_FILES['Product']['name']['thumb'] == null || $_FILES['Product']['name']['box_img'] == null)){
                $this->addErrors(array('Thumbnail or Box Image cannot be blank'));
                return false;
            }

            //allow empty thumb and box image at update time
            if (!$this->isNewRecord)
                return true;

            return true;
        }

        /**
         * After save or insert product, close fancy box
         * */
        public function afterSave(){
        	Yii::app()->clientScript->registerScript(
        		"afterSaveProduct",
        		"$().ready(function(){
                            parent.$.fancybox.close();
                        });");
        }

}